﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using valhalla.Common;


namespace valhalla
{
    /// <summary>
    /// Interaction logic for MainForm.xaml
    /// </summary>
    public partial class MainForm : Window
    {
        private string fileDir = System.Environment.CurrentDirectory + "/Data/";
        private string filename = string.Empty;
        
        
        private DataIntoXML _dataIntoXml = null;

        public MainForm()
        {
            InitializeComponent();
            filename = fileDir + "polygon.xml";
            _dataIntoXml = new DataIntoXML(filename);
            if (!System.IO.Directory.Exists(fileDir))
            {
                System.IO.Directory.CreateDirectory(fileDir);
            }

            LoadData();           
            
        }

        private Dictionary<string, CmmnProp> _dictPolygons = new Dictionary<string, CmmnProp>();
        private string selectedPolygonName = string.Empty;

        private void LoadData()
        {
            int cont = 0;
            foreach (ASPNETX_WPFPolygon item in _dataIntoXml.GetPolygons())
            {
                cont++;
                item.PolygonBody.Name = "P_" + cont.ToString();
                SetPolygon(item);                
                _dictPolygons[item.PolygonBody.Name] = item.CommonProperty;
                previewCanvas.Children.Add(item.PolygonBody);
            }
        }

        private void SetPolygon(ASPNETX_WPFPolygon item)
        {
            item.PolygonBody.MouseEnter += new MouseEventHandler(PolygonMouseEnter);
            item.PolygonBody.MouseMove += new MouseEventHandler(PolygonMouseMove);
            item.PolygonBody.MouseLeave += new MouseEventHandler(PolygonMouseLeave);
            item.PolygonBody.MouseLeftButtonUp += new MouseButtonEventHandler(PolygonClick);
            item.PolygonBody.Stroke = new SolidColorBrush(Colors.Black);
            item.PolygonBody.StrokeThickness = 2;
            item.PolygonBody.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Constants.POLYGON_COLOR));
            item.PolygonBody.Opacity = 0.3;
        }

        


        public void PolygonClick(object sender, MouseButtonEventArgs e)
        {
            Polygon pgnTmp = null;
            if (!selectedPolygonName.Equals(string.Empty))
            {
                foreach (UIElement item in previewCanvas.Children)
                {
                    if (item is Polygon)
                    {
                        pgnTmp = (Polygon)item;
                        pgnTmp.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Constants.POLYGON_COLOR));
                    }
                }
            }

            pgnTmp = (Polygon)sender;
            selectedPolygonName = pgnTmp.Name;
            pgnTmp.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Constants.SELECTED_POLYGON_COLOR));
        }

        public void PolygonMouseLeave(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            polygon.Cursor = Cursors.Arrow;
            pgnTooltip.Hide();
            if (polygon.Name != selectedPolygonName)
                polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Constants.POLYGON_COLOR));
        }


        public void PolygonMouseMove(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            pgnTooltip.Show(e.GetPosition(previewCanvas));

        }

        public void PolygonMouseEnter(object sender, MouseEventArgs e)
        {
            Polygon polygon = ((Polygon)sender);
            CmmnProp cmnProp = _dictPolygons[polygon.Name];
            pgnTooltip.Title = cmnProp.Content;
            pgnTooltip.Description = cmnProp.Detail;
            polygon.Cursor = Cursors.None;
            polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Constants.SELECTED_POLYGON_COLOR));
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
        
        }

        private void viewRectTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void rbPoint_Checked(object sender, RoutedEventArgs e)
        {
            
        }

        private void rbDraw_Checked(object sender, RoutedEventArgs e)
        {
            
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            About aboutFrom = new About();
            aboutFrom.ShowDialog();
        }
    }
}